package me.sdevil507.supports.global.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Collections;

/**
 * 设置SpringMvc全局的ResponseBody返回相关配置
 *
 * @author sdevil507
 * created on 2020/2/25
 */
@Configuration
public class ResponseBodyConfig {

    /**
     * 导入jackson转换bean
     */
    @Resource
    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;

    /**
     * 设置SpringMvc中ResponseBody返回实体时jackson执行转换后对应的MediaType(ContentType)类型
     * 如果不设置将默认是:["application/json"],而我们期待的是:["application/json;charset=UTF-8"]
     * 因为"application/json"类型时,被logbook框架解析时会存在中文乱码
     */
    @PostConstruct
    public void setJacksonMediaType() {
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(new MediaType("application", "json", StandardCharsets.UTF_8)));
    }
}
